// $Id: IdentityMap.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 NourY Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Salamanca.DataAccess;

namespace Salamanca.NUnit.DataAccess
{
    [TestClass]
    public class IdentityMapFixture
    {

        [TestMethod]
        public void CanRegisterObjectAndFindItByKey()
        {
            IdentityMap<object, object> im=new IdentityMap<object, object>();

            object o=new object();
            object o2=null;

            Assert.IsFalse(im.TryGetValue("foo", out o2));
            Assert.IsNull(o2);

            im.Add("foo", o);
            Assert.IsTrue(im.Contains("foo"));
            Assert.IsTrue(im.TryGetValue("foo", out o2));
            Assert.AreSame(o, o2);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void AddingToSameKeyTwiceAlwaysThrows()
        {
            IdentityMap<object, object> im=new IdentityMap<object, object>();
            object o=new object();

            im.Add("foo", o);
            im.Add("foo", o);
        }

        [TestMethod]
        public void CanAddItemAfterPreviousItemIsCollected()
        {
            IdentityMap<object, object> im=new IdentityMap<object, object>();
            im.Add("foo", new object());

            GC.Collect();
            im.Add("foo", new object());
        }

    }
}
